#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/input.h>
#include "jpeglib.h"
#include <stdbool.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>


#define Static_Slide  	0
#define Up_Slide 		1
#define Down_Slide  	2
#define Left_Slide  	3
#define Right_Slide  	4
#define TS_PATH "/dev/input/event0"

//结构体声明
typedef struct node
{
	char name[100];
	struct node * next;
	struct node * prev;
}ND,*PND;
//全局变量
unsigned char *fb_mem;
struct fb_var_screeninfo var;	/* Current var */
struct fb_fix_screeninfo fix;	/* Current fix */
int fb_fd;
int x_res, y_res;
int line_length;
int screen_size;
int pixel_length;
int x1, ys, x2, y2;
int fd;
int x,y;



//打开LCD屏
void lcd_init(void)
{
	int ret;
	/* 打开 framebuffer */
	fb_fd = open("/dev/fb0" , O_RDWR);
	if(fb_fd < 0)
	{
		printf("neo: cannot open the fb device\n");
		return ;
	}

	/*获得固定参数 和 变化参数 */
	ret = ioctl(fb_fd , FBIOGET_VSCREENINFO , &var);
	if(ret)
	{
		printf("neo: get FBIOGET_VSCREENINFO args error");
		return  ;
	}
	
	ret = ioctl(fb_fd , FBIOGET_FSCREENINFO , &fix);
	if(ret)
	{
		printf("neo: get FBIOGET_FSCREENINFO args error");
		return  ;
	}

	line_length	 =  fix.line_length;
	screen_size  =  fix.smem_len;
	pixel_length =  var.bits_per_pixel / 8;
	x_res = var.xres;
	y_res = var.yres;
	
	
	printf("fix.line_length 	= %d\n",fix.line_length);
	printf("fix.smem_len 		= %d\n",fix.smem_len);
	printf("var.bits_per_pixel 	= %d\n",var.bits_per_pixel);
	printf("var.xres = %d\n",var.xres);
	printf("var.yres = %d\n",var.yres);
	printf("screen_size = %d\n",screen_size); //3072000  800*960
	/* 映射 framebuffer 地址 */
    fb_mem =  (unsigned char*)mmap(NULL, screen_size,PROT_READ | PROT_WRITE,MAP_SHARED,fb_fd, 0);
	if(fb_mem == MAP_FAILED)
	{
		printf("mmap failure!\n");
		return;
	}	

}
//展示bmp图片（功能未实现）
void show_bmp(char *pathname,int start_x,int start_y)
{
	int bmp_fd,i,x,y;
	unsigned short heigh,weight;
	unsigned char head_buff[54];//存储头信息的buff
	//打开屏幕
	bmp_fd = open(pathname,O_RDWR);
	if(bmp_fd == -1)
	{
		printf("open bmp failure!\n");
		return ;
	}
	
	read(bmp_fd,head_buff,54);
	//得到图片宽度
	weight = head_buff[19]<<8 | head_buff[18];
	
	//得到图片高度
	heigh = head_buff[23]<<8 | head_buff[22];
	
	
	if((weight+start_x) > 800)
	{
		printf("bmp weight+start_x big 800\n");
		return ;
	}
	
	if((heigh+start_y) > 480)
	{
		printf("bmp heigh+start_y big 800\n");
		return ;
	}
	
	
	printf("weight:%d,heigh:%d\n",weight,heigh);
	
	//定义用于存放图片rgb的buff
	unsigned char bmp_buff[weight*heigh*3];
	unsigned char buff[weight*heigh*3];
    
	
	//将图片像素读到buffer当中
	read(bmp_fd,bmp_buff,sizeof(bmp_buff));	
	
	
	
	for(y=0; y<heigh; y++)
	{
		for(x=0; x<weight*3; x++)
		{
			buff[y*weight*3+x] = bmp_buff[(heigh - 1 -y)*weight*3+x];
		}
	}
	
	for(y=0; y<heigh; y++)
	{
		for(x=0; x<weight; x++)
		{
			*(fb_mem+(start_y + y)*800*4+	start_x*4 +4*x+0)= buff[y*weight*3+3*x+0];
			*(fb_mem+(start_y + y)*800*4+	start_x*4 +4*x+1)= buff[y*weight*3+3*x+1];
			*(fb_mem+(start_y + y)*800*4+	start_x*4 +4*x+2)= buff[y*weight*3+3*x+2];
			*(fb_mem+(start_y + y)*800*4+	start_x*4 +4*x+3)= 0x00;
			
		}
		
	}	
	
	
	
	close(bmp_fd);
}

/*获取滑动方向: 0:表示不滑，1上滑，2下滑，3，左滑，4右滑*/
int Get_Slide(void)
{
	int flags = 0;
	struct input_event xy;
	//1,获取滑动过程中的起点坐标  	-->第一个点的坐标
		//1）打开触摸屏，读取触摸屏中的数据
		int ts_fd = open(TS_PATH, O_RDWR);
		if(-1 == ts_fd)
		{
			perror("open ts failed");
			return -1;
		}
		//2）获取坐标值	-->获取起点坐标
		while(1)
		{
						
			read(ts_fd, &xy, sizeof(xy));
										
			if(xy.type == EV_ABS && xy.code == ABS_X && flags == 0)
			{
				x1 = xy.value * 800/1024;
				flags = 1;
			}
			if(xy.type == EV_ABS && xy.code == ABS_Y && flags == 1)
			{
				ys = xy.value * 480/600;
				flags = 2;
			}
			if(flags == 2)
			{
				flags = 0;
				printf("起点坐标(%d,%d)\n", x1, ys);
				break;
			}
			
		}
		x2 = x1;
		y2 = ys;
	//2,获取滑动过程中的终点坐标	-->手指离开
		while(1)
		{
			read(ts_fd, &xy, sizeof(xy));
			if(xy.type == EV_ABS && xy.code == ABS_X)
			{
				x2 = xy.value * 800/1024;		//手指如果只是点击屏幕，不滑动的情况下，x2,y2是没有数值的
			}
			if(xy.type == EV_ABS && xy.code == ABS_Y)
			{
				y2 = xy.value * 480/600;
			}
			if(xy.type == EV_KEY && xy.code == BTN_TOUCH && xy.value == 0)	//设置结束条件：手指离开屏幕时（压力值为0），结束坐标的获取
			{
				printf("终点坐标(%d,%d)\n", x2, y2);
				break;
			}
		}
	//3,判断两个坐标之间的相对位置
	if(x1 == x2 && ys == y2)	//不滑
	{
		printf("不滑\n");
		return Static_Slide;
	}		
	if(ys>y2 && (ys-y2)*(ys-y2) > (x1-x2)*(x1-x2))	//上滑
	{
		printf("上滑\n");
		return Up_Slide;
	}		
	if(ys<y2 && (ys-y2)*(ys-y2) > (x1-x2)*(x1-x2))	//下滑
	{
		printf("下滑\n");
		return Down_Slide;
	}		
	if(x1>x2 && (x1-x2)*(x1-x2) > (ys-y2)*(ys-y2))	//左滑
	{
		printf("左滑\n");
		return Left_Slide;	
	}	
	if(x2>x1 && (x1-x2)*(x1-x2) > (ys-y2)*(ys-y2))	//右滑
	{
		printf("右滑\n");
		return Right_Slide;		
	}	

	close(ts_fd);
}


//获取坐标
bool getxy(int *px,int *py)
{
    int z;
    struct input_event ev;
    int flag = 0;
    
    while(1)
    {
        z = read(fd,&ev,sizeof(ev));
        if (z < 0)
        {
            
            return false;
        }
        
        if (ev.type == EV_ABS && ev.code == ABS_X)
        {
            *px = ev.value;
            flag = 1;
        }
        
        if (ev.type == EV_ABS && ev.code == ABS_Y)
        {
            *py = ev.value;
            if (flag)
            {
                return true;
            }
            
        }

    }
    
}

//展示jpg图片
bool show_jpeg(char *filename)
{
    
    FILE *fp;
    int fd;
    struct fb_var_screeninfo screeninfo;//声明一个与屏幕信息相关的结构体对象
    //int screenWidth,screenHeight;
    //int bytesperpixel;
    unsigned char *pLineColor;
    unsigned int (*pFrameBuffer)[800];
    int image_width,image_height;
   
   //声明一个jpeg解压对象
    struct jpeg_decompress_struct dinfo;
    //把jpeg的出错信息关联到进程的标准错误设备上，除非你不关心jpeg库的出错信息
    struct jpeg_error_mgr err;
	
    dinfo.err=jpeg_std_error(&err);
    //创建一个jpeg解压对象
    jpeg_create_decompress(&dinfo);

    /*
    if(argc<2)
    {
        printf("Usage:digitalPhoto  jpeg_filename.\n");
        exit(-1);
    }
    */


    fp=fopen(filename,"r");
    if(fp==NULL)
    {
        perror("Cannot open jpeg_file:");
        goto exit;
    }
	
	
//将jpeg解压对象和jpeg文件流关联起来，意味这jpeg解压对象将从文件流中获取数据
    jpeg_stdio_src(&dinfo,fp);
	
//获取头部信息
    jpeg_read_header(&dinfo,TRUE);
    image_height=dinfo.image_height;
    image_width=dinfo.image_width;
    printf("w=%d,h=%d,num=%d\n",image_width,image_height,dinfo.num_components);
	
//打开framebuffer
    fd=open("/dev/fb0",O_RDWR);
    //ioctl(fd,FBIOGET_VSCREENINFO,&screeninfo);
    //screenWidth=screeninfo.xres;//获取屏幕的宽
    //screenHeight=screeninfo.yres;//获取屏幕的高
    //bytesperpixel=screeninfo.bits_per_pixel/8;//获取每个像素颜色所占的字节数


    pFrameBuffer=(unsigned int (*)[800])mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	
	//设置解压参数
	dinfo.scale_num = 1;
	dinfo.scale_denom = 1;
	dinfo.out_color_space=JCS_RGB;
	
//开始解压
    jpeg_start_decompress(&dinfo);
	
//申请一个保存jpeg一行颜色数据的缓冲区
    pLineColor=(unsigned char*)malloc(image_width*dinfo.num_components);
//按行读取jpeg图片的数据并处理
    while(dinfo.output_scanline<dinfo.output_height &&
          dinfo.output_scanline<480)  //output_scanline随着读取的进行会自动+1
    {
        int col;
        unsigned char *pTmpColor=pLineColor;
        unsigned char red,green,blue;

        //读取一行
        jpeg_read_scanlines(&dinfo,&pLineColor,1);
        //处理一行
        for(col=0;col<800&&col<dinfo.image_width;col++)
        {
            red=*pTmpColor;
            green=*(pTmpColor+1);
            blue=*(pTmpColor+2);
            pTmpColor+=3;
           // *(pFrameBuffer+(dinfo.output_scanline-1)*800+col)=(red<<16)|(green<<8)|blue;
            pFrameBuffer[dinfo.output_scanline-1][col]=(red<<16)|(green<<8)|blue;
        }
    }
	
    jpeg_finish_decompress(&dinfo);//完成解压
	
    munmap(pFrameBuffer,800*480*4);
    free(pLineColor);
    fclose(fp);
    close(fd);

exit:
    jpeg_destroy_decompress(&dinfo);//销毁解压对象
  

}

//触摸后展开操作
void touchs(char * picname)
{
    fd = open("/dev/input/event0", O_RDWR|O_NONBLOCK);//打开触摸板
    if (fd == -1)
    {
        perror("fail to open it.\n");
        exit(-1);
    }
    
    fb_mem = mmap(NULL,800*480*3+54,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);



 
    show_jpeg(picname);
    

    
    char num[20];
    sprintf(num,"./%s","1.jpg");


    char password[10];
    bzero(password,10);
    int i = 0;
    
    while (1)
    {

            if (getxy(&x,&y))
            {    
                show_jpeg(num);
                printf("(%d,%d)\n",x,y);
            }  

            else   
            {
                continue;
            } 

                    
            if (x > 300 && x < 340 && y > 335 && y < 435)
            {
                printf("(%d,%d)\n",x,y);
                password[i] = '1';
                i++;
                continue;      
            }

            if (x > 375 && x < 425 && y > 335 && y < 435)
            {
                    
                printf("(%d,%d)\n",x,y);
                password[i] = '2';
                i++;
                continue;  
            }
                    
            if (x > 450 && x < 500 && y > 335 && y < 435)
            {
                    
                printf("(%d,%d)\n",x,y);
                password[i] = '3';
                i++;
                continue;        
            }

            if (x > 535 && x < 580 && y > 335 && y < 435)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '4';
                i++;
                continue;        
            }

            if (x > 625 && x < 665 && y > 335 && y < 435)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '5';
                i++;
                continue;       
            }

            if (x > 300 && x < 340 && y > 465 && y < 580)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '6';
                i++;
                continue;        
            }

            if (x > 375 && x < 425 && y > 465 && y < 580)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '7';
                i++;
                continue;  
            }

            if (x > 450 && x < 500 && y > 465 && y < 580)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '8';
                i++;
                continue;          
            }

            if (x > 535 && x < 580 && y > 465 && y < 580)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '9';
                i++;
                continue;          
                        
            }
                    
            if (x > 625 && x < 665 && y > 465 && y < 580)
            {
                        
                printf("(%d,%d)\n",x,y);
                password[i] = '0';
                i++;
                continue;          
            }
            //wchar_t *wchar = L"*";
	        //show_hanzi(wchar,100,0xffff00, 0,200,350);
            //char bmpn[20] = "fwq.bmp";
            //show_bmp(bmpn,100,100);

            if (i = 4 && strcmp(password,"0807") == 0)
            {
                printf("密码正确！\n");
                break;
            }
            else
                bzero(password,10);
                i = 0;
                printf("请重新输入密码！\n");
                continue;
    }

}


//创建节点
PND make_node(char *picname)
{
	PND pnew;
	pnew = (PND)malloc(sizeof(ND));
	if(pnew == NULL)
	{
		printf("make new node  fail\n");
		return NULL;
	}
	pnew->next = pnew;
	pnew->prev = pnew;
	strcpy(pnew->name,picname);
	
	return pnew;
	
	
}
//头插法
bool list_add_head(PND phead,PND pnew)
{

    if(phead == NULL || pnew == NULL)
		return false;
	
    pnew->prev = phead;
	pnew->next = phead->next;
	phead->next->prev = pnew;
	phead->next = pnew;
	return true;


}


void auto_show(PND phead)
{
    while(1)
    {

        if (getxy(&x,&y))
        {
            printf("(%d,%d)",x,y);
        }

        PND p =phead;
        char album[30];
        sprintf(album,"./dir/%s",phead -> name);
        int t1 = time(NULL);
        show_jpeg(album);
        while(1)
        {
            if (getxy(&x,&y))
            {
                printf("(%d,%d)",x,y);
            }
            
            if (x < 600 && x > 400 && y < 100 && y > 0)
            {
                int t2 = time(NULL);
                if (t2 - t1 >= 2)
                {        
                    p = p -> next;
                    bzero(album,30);
                    sprintf(album,"./dir/%s",p -> name);
                    show_jpeg(album);
                    printf("pic is :%s\n", p -> name);
                    t1 = time(NULL); 
                } 
            }    
            
            if (x > 400 && x < 600 && y < 600 && y > 500)
            {
                return;
            }
                    
                
        }
        


    }
}



//手动相册的展示
int album(PND phead)
{

    PND p =phead;
    char album[30];
    sprintf(album,"./dir/%s",phead -> name);
    while(1)
    {
        if (getxy(&x,&y))
        {
            show_jpeg(album);
            printf("(%d,%d)\n",x,y);
        }

        int dir = Get_Slide();
        
        if (dir == 4)
        {
            p = p -> next;
            bzero(album,30);
            sprintf(album,"./dir/%s",p -> name);
            show_jpeg(album);
                

        }
            
        if (dir == 3)
        {
            p = p -> prev;
            bzero(album,30);
            sprintf(album,"./dir/%s",p -> name);
            show_jpeg(album);

        }

        if (x < 600 && x > 400 && y < 600 & y > 500)
        {
            return 1;
        }      
        
    }


}

int play_music(void)
{
    char music[20];
    bzero(music,20);
    sprintf(music,"./%s","music.jpg");
    char st[50];
    bzero(st,50);
    sprintf(st,"killall -STOP madplay");
    show_jpeg(music);
    char kill[50];
    bzero(kill,50);
    sprintf(kill,"killall  -KILL  madplay");
    char cotn[50];
    bzero(cotn,50);
    sprintf(cotn,"killall -CONT madplay");
    while(1)
    {
        if (getxy(&x,&y))
        {
            printf("(%d,%d)\n",x,y);
            
            //打开播放列表
            if (x > 0 && x < 150 && y > 450 && y < 600)
            {
                char ml[20];
                bzero(ml,20);
                sprintf(ml,"./%s","musiclist.jpg");
                show_jpeg(ml);
                while(1)
                {
                    if (getxy(&x,&y))
                    {
                        printf("(%d,%d)\n",x,y);
                        char m_o[50];
                        if (x > 370 && x < 540 && y > 125 && y < 150)
                        {
                            system(kill);
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","6.mp3");
                            system(m_o);
                        }
                        
                        if (x > 370 && x < 570 && y > 150 && y < 200)
                        {
                            system(kill); 
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","1.mp3");
                            system(m_o);
                        }
                        
                        if (x > 370 && x < 640 && y > 200 && y < 260)
                        {
                            system(kill);
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","2.mp3");
                            system(m_o);
                        }

                        if (x > 370 && x < 540 && y > 260 && y < 300)
                        {
                            system(kill);
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","3.mp3");
                            system(m_o);
                        }
                        
                        if (x > 370 && x < 555 && y > 300 && y < 330)
                        {
                            system(kill);
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","5.mp3");
                            system(m_o);
                        }

                        if (x > 370 && x < 480 && y > 330 && y < 350)
                        {
                            system(kill);
                            bzero(m_o,50);
                            sprintf(m_o,"madplay -a -25 ./music/%s &","4.mp3");
                            system(m_o);
                        }

                        if (x > 0 && x < 150 && y > 0 && y < 150)
                        {
                            break;
                        }
                        

                    }
                    
                }
            }
            
            //播放
            if (x > 480 && x < 540 && y > 480 && y < 600)
            {
                
                system(kill);
                char m_o[50];
                bzero(m_o,50);
                sprintf(m_o,"madplay -a -25 ./music/%s &","1.mp3");
                system(m_o);

                while(1)
                {
                    if (getxy(&x,&y))
                    {
                        //暂停
                        if (x > 480 && x < 540 && y > 480 && y < 600)
                        {
                            char musicst[20];
                            bzero(musicst,20);
                            sprintf(musicst,"./%s","musicstop.jpg");
                            show_jpeg(musicst);
                            system(st);
                            while(1)
                            {
                                if (getxy(&x,&y))
                                {
                                    printf("(%d,%d)\n",x,y);
                                    if (x > 480 && x < 540 && y > 480 && y < 600)
                                    {
                                        system(cotn);
                                        char ml[20];
                                        bzero(ml,20);
                                        sprintf(ml,"./%s","musiclist.jpg");
                                        show_jpeg(ml);
                                    }
                                    
                                    if (x > 0 && x < 150 && y > 0 && y < 150)
                                    {
                                        break;
                                    }

                                    if (x > 874 && x < 1024 && y > 0 && y < 150)
                                    {
                                        return 0;
                                    }

                                    char m_o[50];
                                    if (x > 370 && x < 540 && y > 125 && y < 150)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","6.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 570 && y > 150 && y < 200)
                                    {
                                        system(kill); 
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","1.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 640 && y > 200 && y < 260)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","2.mp3");
                                        system(m_o);
                                    }

                                    if (x > 370 && x < 540 && y > 260 && y < 300)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","3.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 555 && y > 300 && y < 330)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","5.mp3");
                                        system(m_o);
                                    }

                                    if (x > 370 && x < 480 && y > 330 && y < 350)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","4.mp3");
                                        system(m_o);
                                    }

                                }
                                
                            }
                            
                        }
                        
                        //下一首
                        if (x > 580 && x < 650 && y > 480 && y < 600)
                        {
                            system(kill);
                            char m_t[50];
                            bzero(m_t,50);
                            sprintf(m_t,"madplay -a -25 ./music/%s &","2.mp3");
                            system(m_t);
                        }

                        //上一首
                        if (x > 370 && x < 450 && y > 480 && y < 600)
                        {
                            system(kill);
                            char m_s[50];
                            bzero(m_s,50);
                            sprintf(m_s,"madplay -a -25 ./music/%s &","6.mp3");
                            system(m_s);
                        }
                        
                        //返回
                        if (x > 0 && x < 150 && y > 0 && y < 150)
                        {
                            break;
                        }

                        //home键
                        if (x > 874 && x < 1024 && y > 0 && y < 150)
                        {
                            return 0;
                        }

                        //播放列表
                        if (x > 0 && x < 150 && y > 450 && y < 600)
                        {
                            char msl[20];
                            bzero(msl,20);
                            sprintf(msl,"./%s","musicstoplist.jpg");
                            show_jpeg(msl);
                            while(1)
                            {
                                if (getxy(&x,&y))
                                {
                                    printf("(%d,%d)\n",x,y);
                                    char m_o[50];
                                    if (x > 370 && x < 540 && y > 125 && y < 150)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","6.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 570 && y > 150 && y < 200)
                                    {
                                        system(kill); 
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","1.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 640 && y > 200 && y < 260)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","2.mp3");
                                        system(m_o);
                                    }

                                    if (x > 370 && x < 540 && y > 260 && y < 300)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","3.mp3");
                                        system(m_o);
                                    }
                                    
                                    if (x > 370 && x < 555 && y > 300 && y < 330)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","5.mp3");
                                        system(m_o);
                                    }

                                    if (x > 370 && x < 480 && y > 330 && y < 350)
                                    {
                                        system(kill);
                                        bzero(m_o,50);
                                        sprintf(m_o,"madplay -a -25 ./music/%s &","4.mp3");
                                        system(m_o);
                                    }

                                    if (x > 0 && x < 150 && y > 0 && y < 150)
                                    {
                                        break;
                                    }
                                }

                            }
                        }
                        
                        

                    }
                }
            }

            if (x > 0 && x < 150 && y > 0 && y < 150)
            {
                return 1;
                
            }
            
            if (x > 874 && x < 1024 && y > 0 && y < 150)
            {
                return 0;
            }
            
        }
    }
}

int main(int argc, char *argv[])
{
    if(argc !=2 )
	{
		printf("usage:%s dirname\n",argv[0]);
		exit(1);
	}
    char home[20];
    bzero(home,20);
    sprintf(home,"./%s","home.jpg");
    label:
    touchs(home);
    
    



    struct stat fileinfo;
	stat(argv[1],&fileinfo);
    
    if(!S_ISDIR(fileinfo.st_mode))//不是目录文件
	{
		printf("not a dir\n");
		exit(1);	
	}

	DIR * pdir = opendir(argv[1]);//打开目录文件，并不是进入目录
	
    PND phead = NULL;
    PND pnew = NULL;
    int q = 1;
	struct dirent* ep;
	ep = readdir(pdir);//每读取一次获得一个目录项目
    while(ep != NULL)
    {
        if (strcmp(ep -> d_name,".") && strcmp(ep->d_name,".."))
        {
            if (q == 1)
            {
                printf("%s\n",ep -> d_name);
                phead = make_node(ep -> d_name);
                
                q++;
            }
            else
            {
                
                pnew = make_node(ep -> d_name);
                printf("%s\n",pnew ->name);
                list_add_head(phead,pnew);
            }
        }
        ep = readdir(pdir);
        
    }
	
    
    
    char chpic[20] = "choice.jpg";
    choice:
    show_jpeg(chpic);

    while(1)
    {

        if (getxy(&x,&y))
        {

            
            if (x > 756 && x < 1024 && y > 480 && y < 600)
            {
                goto label;
            }
            
            
            if (x > 0 && x < 512 && y > 0 && y < 600)
            {
                char m_c[30];
                bzero(m_c,30);
                sprintf(m_c,"./%s","mode_choice.jpg");
                show_jpeg(m_c);

                
                while(1)
                {
                    if (getxy(&x,&y))
                    {
                        if (x > 0 && x < 512 && y > 0 && y < 600)
                        {
                            int ret = album(phead);
                    
                            if (ret == 1)
                            {
                                break;
                            }
                            
                        }

                        if (x > 512 && x < 1024 && y > 0 && y < 600)
                        {
                            auto_show(phead);
                            break;
                        }
                        
                        
                    }
                    

                }
                goto choice;

                
            }

            if (x > 512 && x < 1024 && y > 0 && y < 600)
            {
                int retmusic = play_music();
                if (retmusic == 0)
                {
                    goto label;
                }
                if (retmusic == 1)
                {
                    goto choice;
                }
                
                
            }
            

        }

        

    }

   
    return 0;
}

